//
//  ST_RecommendForYouHeaderView.swift
//  btvideo
//
//  Created by mac on 2020/11/30.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit



class ST_RecommendForYouHeaderView: UICollectionReusableView {
    
    static let itemWidth = (ScreenWidth - 10 * 4)/3
    
    static let ratio = ST_RecommendForYouHeaderView.itemWidth/112
    
    static let itemHeight = 60*ST_RecommendForYouHeaderView.ratio
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        [item0,item1,item2].forEach{ addSubview($0) }
        
        
        item0.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(0)
            make.width.equalTo(ST_RecommendForYouHeaderView.itemWidth)
        }
        
        item1.snp.makeConstraints { (make) in
            make.left.equalTo(item0.snp.right).offset(10)
            make.top.bottom.width.equalTo(item0)
        }
        
        item2.snp.makeConstraints { (make) in
            make.left.equalTo(item1.snp.right).offset(10)
            make.top.bottom.width.equalTo(item0)
        }
        
        
        
        item0.addTarget(ST_MediaChatDispatcher.shared, action: #selector(ST_MediaChatDispatcher.match_video_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        item1.addTarget(ST_MediaChatDispatcher.shared, action: #selector(ST_MediaChatDispatcher.match_voice_handle(sender:)), for: UIControl.Event.touchUpInside)
        
        item2.addTarget(ST_MediaChatDispatcher.shared, action: #selector(ST_MediaChatDispatcher.match_peculiarity_handle), for: UIControl.Event.touchUpInside)
        
        binding()
        
    }
    
    func binding(){
        
        ST_BTUserInfoManager.shared.user_additional_attributes_update.withUnretained(self).subscribe(onNext: { own,_ in
            
            guard let attributes = ST_BTUserInfoManager.shared.additional_attributes,ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male else {
                
                
                if let str = ST_BTUserInfoManager.shared.additional_attributes?.homeRecommendedHeaderMatchStr,str.count > 1{
                    
                    own.item2.sub_title_label.text = str
                    
                }
                
                return
            }
            
            own.item0.sub_title_label.text = attributes.homeRecommendedHeaderVideoStr
            
            own.item1.sub_title_label.text = attributes.homeRecommendedHeaderVoiceStr
            
            own.item2.sub_title_label.text = attributes.homeRecommendedHeaderMatchStr
            
            
        }).disposed(by: rx.disposeBag)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private let item0 = ST_Recommend_for_you_header_item_view(title: "视频速配", subTitle: "真人视频交友", lotResourceName: "videoMatch_Lot", colors: [UIColor.init(hex: 0x48C6EF)!,UIColor.init(hex: 0x7995F8)!])
    
    private let item1 = ST_Recommend_for_you_header_item_view(title: "语音速配", subTitle: "实时语音聊天", lotResourceName: "voiceMatch_Lot", colors: [UIColor.init(hex: 0xFE576D)!,UIColor.init(hex: 0xFF78A9)!])
    
    private let item2 = ST_Recommend_for_you_header_item_view(title: "聊天速配", subTitle: "多人在线", lotResourceName: "sameCityMatch_Lot_2", colors:  [UIColor.init(hex: 0xFFC95B)!,UIColor.init(hex: 0xFF8B36)!])
    
    
    
    
    
    
    
}



fileprivate class ST_Recommend_for_you_header_item_view:UIControl{
    
    let lotResourceName:String
    let title:String
    let subTitle:String
    
    init(title:String,subTitle:String,lotResourceName:String,colors:[UIColor]) {
        
        self.lotResourceName = lotResourceName
        self.title = title
        self.subTitle = subTitle
        
        super.init(frame: .zero)
        
        self.cornerRadius = 10
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(animateView)
        animateView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(47*ST_RecommendForYouHeaderView.ratio)
            make.top.equalToSuperview().offset(14*ST_RecommendForYouHeaderView.ratio)
        }
        
        addSubview(sub_title_label)
        sub_title_label.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(6*ST_RecommendForYouHeaderView.ratio)
        }
        
        bgView.model = ST_FWGradientViewModel.init(colors: colors, startPoint: CGPoint.init(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var bgView:ST_FWGradientView = {
        let control  = ST_FWGradientView()
        control.isUserInteractionEnabled = false
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = self.title
        control.textColor = UIColor.white
        control.font = .systemFont(ofSize: 13*ST_RecommendForYouHeaderView.ratio, weight: .medium)
        return control
    }()
    
    fileprivate lazy var sub_title_label:UILabel = {
        let control = UILabel.init()
        control.text = self.subTitle
        control.textColor = UIColor.white
        control.font = .systemFont(ofSize: 8*ST_RecommendForYouHeaderView.ratio, weight: .medium)
        return control
    }()
    
    fileprivate lazy var animateView:LOTAnimationView = {
        let path = Bundle.main.path(forResource: lotResourceName, ofType: "json")!
        let v = LOTAnimationView.init(filePath: path)
        //        animateView.contentMode = UIView.ContentMode.scaleAspectFill
        v.loopAnimation = true
        v.isUserInteractionEnabled = false
        return v
    }()
    
    
    override func layoutSubviews(){
        super.layoutSubviews()
        
        if !animateView.isAnimationPlaying{
            animateView.play()
        }
        
    }
    
}
